package com.tedu.egmvc.controller;

import com.tedu.egmvc.mapper.IAddressMapper;
import com.tedu.egmvc.pojo.dto.AddressDTO;
import com.tedu.egmvc.pojo.entity.Address;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Api("地址管理模块")
public class AddressController {
    /*@Autowired(required=true)：
    当使用@Autowired注解的时候，其实默认就是@Autowired(required=true)，表示注入的时候，该bean必须存在，否则就会注入失败。
    @Autowired(required=false)：
    表示忽略当前要注入的bean，如果有直接注入，没有跳过，不会报错。*/
    @Autowired
    private IAddressMapper iAddressMapper;

    @PostMapping("/address/insert")
    @ResponseBody
    @ApiOperation("添加地址")
    public String addAddress(AddressDTO addressDTO){
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO,address);
        int rows = iAddressMapper.insertAddress(address);
        return rows>0?"添加成功":"添加失败";

    }

    @GetMapping("/address/select")
    @ResponseBody
    @ApiOperation("查看所有地址")
    public List<Address> selectAllAddress(){
        List<Address> addresses = iAddressMapper.selectAll();
        return addresses;
    }

    @DeleteMapping("/address/delete/{id}")
    @ResponseBody
    @ApiOperation("删除地址")
    @ApiImplicitParam(name = "id",value = "地址id",dataType = "Integer",required = true,example = "1")
    public String delete(@PathVariable Integer id){
        int rows = iAddressMapper.delete(id);
        return rows>0?"删除成功":"删除失败";

    }

    @PutMapping("/address/update")
    @ResponseBody
    @ApiOperation("修改地址")
    public String update(Address addressT){
        /*Address address = new Address();
        BeanUtils.copyProperties(addressT,address);*/
        int rows = iAddressMapper.update(addressT);
        return rows>0?"修改成功":"修改失败";


    }


   /* @RequestMapping("/test")
    @ResponseBody
    @ApiOperation("测试")
    public void test(Address address){
        System.out.println(address);

    }*/

}
